const Controller = require('egg').Controller;

class VideoController extends Controller {
  async getHots() {
    const tops = await this.service.redishot.topHots(10);
    this.ctx.body = tops;
  }
  async createComment() {
    const body = this.ctx.request.body;
    const videoId = this.ctx.params.videoId;
    this.ctx.validate({
      content: { type: 'string' },
    }, body);
    const { Video, VideoComment } = this.app.model;
    const video = await Video.findById(videoId);
    if (!video) {
      this.ctx.throw(404, '视频不存在');
    }
    const comment = await new VideoComment({
      content: body.content,
      user: this.ctx.user._id,
      video: videoId,
    }).save();
    if (comment) {
      video.commentCount = await VideoComment.countDocuments({
        video: videoId,
      });
      await video.save();
      // 增加热度
      this.service.redisHot.hotInc(videoId, 2);
      this.ctx.body = {
        msg: '评论成功',
      };
    } else {
      this.ctx.throw(501, '视频评论失败');
    }
  }
}
module.exports = VideoController;
